//
//  SetBase64andMd5.m
//  xhtios1.0
//
//  Created by hlt on 15/7/10.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "SetBase64andMd5.h"

#import "GTMBase64.h"
@implementation SetBase64andMd5

//base64编码和解码根据encryptOrDecrypt的值来的
+ (NSString *)base64:(NSString *)str ncryptOrDecrypt:(CCOperation)encryptOrDecrypt
{
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    
    NSString *jieguo;
    if (encryptOrDecrypt == kCCEncrypt)//base64编码
    {
        //编码
        jieguo = [[NSString alloc] initWithData:[GTMBase64 encodeData:data] encoding:NSUTF8StringEncoding];
        
    }
    else   //base64解码
    {
        //解码
        jieguo = [[NSString alloc] initWithData:[GTMBase64 decodeString:str] encoding:NSUTF8StringEncoding];
    }
    return jieguo;
    
}



//将字符串转换为MD5
+ (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result); // This is the md5 call
    NSString *str33= [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
    
    return [str33 uppercaseString];
    
}


//将请求的参数转换为json格式的字符串

+(NSString *) tojson:(NSDictionary *) dict
{
    
    NSArray *ary= [dict allKeys];
    
    NSString *json=@"{";
    for (NSString *key in ary) {
        
        json=[json stringByAppendingFormat:@"\"%@\":\"%@\",",key,dict[key]];
        
    }
    
    
    NSString *cccc = [json substringToIndex:[json length] - 1];
    
      cccc=[cccc stringByAppendingString:@"}"];
    
    return  cccc;
}


@end
